package com.hmdp.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;


public class StringTools {
    private static final Logger logger = LoggerFactory.getLogger(StringTools.class);

    public static boolean isEmpty(String str) {
        if (null == str || "".equals(str) || "null".equals(str) || "\u0000".equals(str)) {
            return true;
        } else if ("".equals(str.trim())) {
            return true;
        }
        return false;
    }
    public int[] twoSum(int[] nums, int target) {
        int[] arr = new int[2];
        Map<Integer,Integer> m = new HashMap<Integer,Integer>();
        for(int i = 0;i<nums.length;i++){
            if(m.containsKey(target - nums[i])){
                arr[0] = m.get(target - nums[i]);
                arr[1] = i;
            }else{
                m.put(nums[i],i);
            }
        }
        return arr;
    }
    public static List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> list = new ArrayList<>();
        Map<String,List<String>>map = new HashMap<>();
        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String s = String.valueOf(chars);//或者使用new String(chars)
            //computeIfAbsent方法key存在返回value,不存在则返回newValue
            map.computeIfAbsent(s,k->new ArrayList<String>()).add(str);
        }
        //遍历Map
        map.forEach((s,list1)->list.add(list1));
        return list;
    }

    public static void main(String[] args) {
        List<List<String>> list = groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
        System.out.println(list);
    }
}